package cease.command.timeline;

import java.util.Collection;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

import cease.butter.TimelineExecutor;
import cease.command.HasOptionCommand;
import cease.console.PrintHelper;
import cease.urs.Session;
import cease.vo.Butter;

/**
 * @author dhf
 */
public class SearchTLCommand extends HasOptionCommand {
    @Option(name = "-r", usage = "record per page", metaVar = "optional")
    private Integer rpp;

    @Option(name = "-p", usage = "page index", metaVar = "optional")
    private Integer page;

    @Option(name = "-w", usage = "query word", metaVar = "required")
    private String word;

    @Option(name = "-s", usage = "time since, format 'yyyy-MM-dd'", metaVar = "optional")
    private String since;

    @Option(name = "-u", usage = "time until, format 'yyyy-MM-dd'", metaVar = "optional")
    private String until;

    private boolean showUser = true;

    private void reset() {
        rpp = null;
        page = null;
        word = null;
    }

    public String getCmdDesc() {
        return "search butters in public timeline";
    }

    public void run(Session session, String[] args) throws Exception {
        try {
            parser.parseArgument(args);
            Collection<Butter> butters = TimelineExecutor.getInstance().search(
                    session, word, rpp, page, since, until, showUser);
            PrintHelper.printButter(butters);
        } catch (CmdLineException e) {
            parser.printUsage(System.out);
        } finally {
            reset();
        }
    }
}
